package com.xzz.mybatis.generator.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzz.mybatis.generator.bean.entity.QrtzRunningLog;
import com.xzz.mybatis.generator.bean.mapper.QrtzRunningLogMapper;
import com.xzz.mybatis.generator.common.MyBaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author: hhz
 * @createDate: 2021-3-29
 */
@Service
public class QrtzRunningLogServiceImpl extends MyBaseServiceImpl<QrtzRunningLogMapper, QrtzRunningLog>
        implements QrtzRunningLogService {
    @Override
    public boolean saveNewLog(QrtzRunningLogDTO newLogDto) {
        QrtzRunningLog newLogEntity = new QrtzRunningLog();
        BeanUtils.copyProperties(newLogDto, newLogEntity);
        return save(newLogEntity);
    }

    @Override
    public IPage<QrtzRunningLogDTO> getLatestByJobKey(String jobKey, int pageSize, int currentPage) {

        QueryWrapper<QrtzRunningLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(QrtzRunningLog::getJobName, jobKey)
                .orderByDesc(QrtzRunningLog::getCreated);
        IPage<QrtzRunningLog> entityPage = page(new Page<>(currentPage, pageSize), queryWrapper);
        IPage<QrtzRunningLogDTO> dtoPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(),
                entityPage.getTotal());
        dtoPage.setRecords(toDtoList(entityPage.getRecords(), QrtzRunningLogDTO.class));
        return dtoPage;
    }
}
